module.exports = function ({UsersWithdraws, UsersFinances, sequelize}) {

    /**
     * 统计分析 V2
     */
    UsersWithdraws.analyseV2 = function () {
        let sql = "select status, count(*) count, ifnull(sum(amount),0) amount from UsersWithdraws group by status";
        return sequelize.query(sql, {
            type: sequelize.QueryTypes.SELECT
        });
    }

    /**
     * 转账成功 V2
     */
    UsersWithdraws.successfulV2 = function (id) {
        return UsersWithdraws.update({status: 'Successful'}, {
            where: {
                id: id,
                status: 'Pending'
            }
        }).then(function (result) {
            return {success: (result[0] > 0)};
        });
    }

    /**
     * 转账失败 V2
     */
    UsersWithdraws.failedV2 = function (id) {
        return sequelize.transaction(function (t) {
            return UsersWithdraws.update({status: 'Failed'}, {
                where: {id: id, status: 'Pending'},
                transaction: t
            }).then(function (result) {
                if (!result[0]) {
                    return {success: false};
                }
                return UsersWithdraws.findOne({where: {id: id}, transaction: t}).then(function (withdraw) {
                    let sql = "update Users u set u.amount = u.amount + :amount where u.id = :user_id";
                    return sequelize.query(sql, {
                        replacements: {
                            amount: withdraw.total_amount,
                            user_id: withdraw.user_id
                        }, transaction: t
                    }).spread(function (res) {
                        if (!res.affectedRows) {
                            throw new Error('更新账户金额失败');
                        }
                        return {success: true};
                    });
                });
            });
        }).catch(function (e) {
            if (e) {
                console.error('\n', e.stack || e.message);
            }
            return {success: false, message: e.message || ''};
        });
    }

    /**
     * 申请提现 V2
     */
    UsersWithdraws.applyV2 = function (withdraw) {
        return sequelize.transaction(function (t) {
            return UsersWithdraws.create(withdraw, {transaction: t}).then(function (usersWithdraw) {
                return UsersFinances.create({
                    partner_id: usersWithdraw.partner_id,
                    user_id: usersWithdraw.user_id,
                    amount: usersWithdraw.total_amount,
                    type: 'Out',
                    operation: 'Withdraw',
                    by: 'bank_type_' + withdraw.bank_type,
                    status: 'Successful'
                }, {transaction: t}).then(function () {
                    let sql = "update Users u set u.amount = u.amount - :amount where u.id = :user_id and u.amount >= :amount";
                    return sequelize.query(sql, {
                        replacements: {
                            user_id: usersWithdraw.user_id,
                            amount: usersWithdraw.total_amount
                        }, transaction: t
                    }).spread(function (res) {
                        if (!res.affectedRows) {
                            throw new Error('更新账户金额失败');
                        }
                        return {success: true, data: usersWithdraw};
                    });
                });
            });
        }).catch(function (e) {
            if (e) {
                console.error('\n', e.stack || e.message);
            }
            return {success: false, message: e.message || ''};
        });
    }

}